package com.changpay.zywy.ui.activity.power

import android.content.Intent
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.activity.freeze.FreezePeopleActivity

import kotlinx.android.synthetic.main.activity_power.*
import kotlinx.android.synthetic.main.title_layout.*

class PowerActivity : BaseActivity(), IView {
    override fun initData() {

    }

    override fun start() {

    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)

    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {

    }


    private var type_set_check: Int = 0
    private var member_id: String = ""


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
            R.id.tv_daili_name -> {
                Intent(this, FreezePeopleActivity::class.java).run {
                    putExtra("type",4)
                    startActivityForResult(this, 100)
                }
            }
            R.id.btn_submit->
            {
                setAgentData()
            }
        }
    }

    override fun attachLayoutRes(): Int = R.layout.activity_power


    override fun initView() {
        tv_title.text = "权限设置"
        iv_back.setOnClickListener(onClickListener)
        tv_daili_name.setOnClickListener(onClickListener)
        btn_submit.setOnClickListener(onClickListener)
        switch_one_view.setOnClickListener {  }
        switch_two_view.setOnClickListener {  }
        type_rg.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.rb_one -> {
                    type_set_check = 0
                    if (member_id.isNotEmpty()) {
                        btn_submit.visibility = View.VISIBLE
                        rv_switch_one.visibility = View.VISIBLE
                        rv_switch_two.visibility = View.VISIBLE
                        switch_one_view.visibility = View.GONE
                        switch_two_view.visibility = View.GONE
                    }
                }
                R.id.rb_two -> {
                    type_set_check = 1
                    if (member_id.isNotEmpty()) {
                        rv_switch_one.visibility = View.VISIBLE
                        rv_switch_two.visibility = View.VISIBLE
                        switch_one_view.visibility = View.VISIBLE
                        switch_two_view.visibility = View.VISIBLE
                        loadAgentData()
                    }
                    btn_submit.visibility = View.GONE
                }
            }
        }

    }

    fun loadAgentData() {
        val params = HashMap<String, String>()
        params["agent_id"] = member_id
        params["key"] = token
        params["client"] = "android"
        RetrofitHelper.service.getSettingMerchantRole(params).sss(this)
        {
            switch_one.isChecked = it.result.rate_setting != 0
            switch_two.isChecked = it.result.develop_setting != 0
        }
    }

    fun setAgentData() {
        val params = HashMap<String, String>()
        params["agent_id"] = member_id
        params["key"] = token
        params["client"] = "android"
        params["rate_setting"] =  if(switch_one.isChecked) "1" else "0"
        params["develop_setting"] =  if(switch_two.isChecked) "1" else "0"
        RetrofitHelper.service.setMerchantRole(params).sss(this)
        {
            showToast(it.result.toString())
        }
    }



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 200) {
            tv_daili_name.text = data?.getStringExtra("member_name")
            tv_daili_phone.text = data?.getStringExtra("member_phone")
            member_id = data?.getStringExtra("member_id")!!
            loadAgentData()
            rv_switch_one.visibility = View.VISIBLE
            rv_switch_two.visibility = View.VISIBLE
            if (type_set_check == 0) {
                btn_submit.visibility = View.VISIBLE
                switch_one_view.visibility = View.GONE
                switch_two_view.visibility = View.GONE
            } else {
                btn_submit.visibility = View.GONE
                switch_one_view.visibility = View.VISIBLE
                switch_two_view.visibility = View.VISIBLE
            }
        }
    }
}
